/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "power.h"
#include <mc/log.h>

namespace dev {

bool Gpu_Power::read_gpu_power_consumption(postbox_ptr postbox) {
    if (!postbox) {
        elog("CardPower: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardPower";
        params.opcode         = 0x4;
        params.arg1           = 0x0;
        params.arg2           = 0x0;
        params.capa_dword_num = 0;
        params.capa_mask      = 0x00010000;
        params.data_out       = true;
        params.data_len       = 4;
        params.data_type      = SMBusPostboxProtocol::KEEP_DIGITAL;
        params.data_mask      = 0xffffffff;

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("CardPower: empty response when reading power");
            return false;
        }
        uint32_t power_milliwatts = static_cast<uint32_t>(std::stoul(result_str));
        uint16_t power_watts      = static_cast<uint16_t>(power_milliwatts / 1000);
        elog("power watts read: ${CardPower}", ("CardPower", power_watts));
        try {
            PowerWatts.set_value(power_watts);
        } catch (const std::exception& e) {
            elog("CardPower Set value failed: ${error}", ("error", e.what()));
        }
        return true;
    } catch (const std::exception& e) {
        elog("CardPower: exception when reading CardPower: ${e}", ("e", e.what()));
        return false;
    }
}
bool Gpu_Power::read_gpu_powerbrakeIsSet(postbox_ptr postbox) {
    if (!postbox) {
        elog("CardPowerBrakeIsSet: postbox not initialized");
        return false;
    }
    try {
        SMBusPostboxProtocol::RequestParams params;
        params.name           = "CardPowerBrakeIsSet";
        params.opcode         = 0xF5;
        params.arg1           = 0x00;
        params.arg2           = 0x00;
        params.capa_dword_num = 3;              // 与 Lua 对齐 dword3
        params.capa_mask      = 0x00000020;     // 能力位
        params.data_out       = true;
        params.data_len       = 1;
        params.data_type      = SMBusPostboxProtocol::KEEP_DIGITAL;
        params.data_mask      = 0x01;           // 低 1 位

        std::string result_str = postbox->sendRequest(params);
        if (result_str.empty()) {
            elog("CardPowerBrakeIsSet: empty response");
            return false;
        }
        uint32_t rawvalue = static_cast<uint32_t>(std::stoul(result_str));
        elog("CardPowerBrakeIsSet rawvalue read: ${CardPowerBrakeIsSet}", ("CardPowerBrakeIsSet", rawvalue));
        // KEEP_DIGITAL 已掩码+右移归一化，这里应为 "0" 或 "1"
        uint8_t val = static_cast<uint8_t>(rawvalue & 0xFF);
        PowerBrakeState.set_value(val);
        return true;
    } catch (const std::exception& e) {
        elog("CardPowerBrakeIsSet: exception when reading: ${e}", ("e", e.what()));
        return false;
    }
}

} // namespace dev

MC_REFLECT(dev::Gpu_Power)